package net.vkits.api;

import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiParam;
import net.vkits.anjiamiao.application.DeviceApplicationService;
import net.vkits.common.dto.BaseDTO;
import net.vkits.common.dto.DeviceDTO;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by ethan-liu on 2016/11/23.
 */
@Controller
public class DeviceApiController implements DeviceApi {
    private static final Logger logger = LoggerFactory.getLogger(DeviceApiController.class);

    @Resource
    private DozerBeanMapper mapper;
    @Resource
    private HttpServletRequest request;
    @Resource
    private DeviceApplicationService deviceApplicationService;

    @Override
    public ResponseEntity<BaseDTO> updateDevice(@ApiParam(value = "设备信息", required = true) @RequestBody DeviceDTO deviceDTO) {
        final Claims claims = (Claims) request.getAttribute("claims");
        String cellphone = claims.get("phone").toString();
        logger.info("get device info: {}", deviceDTO);
        deviceApplicationService.addOrUpdateDevice(cellphone,deviceDTO);
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setCode(200);
        baseDTO.setMsg("OK");
        return new ResponseEntity<>(baseDTO, HttpStatus.OK);
    }
}
